package dao;

import common.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * created by YT
 * description: 封装了针对problem 的增删改查
 * User:lenovo
 * Data:2022-07-18
 * Time:15:23
 */
public class ProblemDAO {
    // 1. 增加题目
    public static int insertProblem(Problem problem) {
        Connection connection = null;
        PreparedStatement statement = null;
        int ret = 0;
        try {
            connection = DBUtil.getConnection();

            String sql = "insert into oj_table value(null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, problem.getTitle());
            statement.setString(2, problem.getLevel());
            statement.setString(3, problem.getDescription());
            statement.setString(4, problem.getTemplateCode());
            statement.setString(5, problem.getTestCode());
            ret = statement.executeUpdate();
            //System.out.println(ret);
            if (ret == 1) {
                System.out.println("增加成功");
            } else {
                System.out.println("增加失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
        return ret;

    }
    // 2. 删除题目
    public static int deleteProblem(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        int ret = 0;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return ret;
    }
    // 3. 查询题目列表
    public static List<Problem> findAllProblems() {
        List<Problem> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select id,title,level from oj_table";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String level = resultSet.getString("level");
                Problem problem = new Problem();
                problem.setId(id);
                problem.setTitle(title);
                problem.setLevel(level);
                list.add(problem);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return list;
    }

    // 4. 查询题目详情
    public static Problem findProblem(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Problem problem = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                //int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String level = resultSet.getString("level");
                String description = resultSet.getString("description");
                String templateCode = resultSet.getString("templateCode");
                String testCode = resultSet.getString("testCode");
                problem = new Problem();
                problem.setId(id);
                problem.setTitle(title);
                problem.setLevel(level);
                problem.setDescription(description);
                problem.setTemplateCode(templateCode);
                problem.setTestCode(testCode);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return problem;
    }

    public static void main(String[] args) {
        Problem problem = new Problem();
        problem.setTitle("两数之和");
        problem.setLevel("简单");
        problem.setDescription("给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target  的那 两个 整数，并返回它们的数组下标。\n" +
                "\n" +
                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
                "\n" +
                "你可以按任意顺序返回答案。\n" +
                "\n" +
                " \n" +
                "\n" +
                "示例 1：\n" +
                "\n" +
                "输入：nums = [2,7,11,15], target = 9\n" +
                "输出：[0,1]\n" +
                "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n" +
                "示例 2：\n" +
                "\n" +
                "输入：nums = [3,2,4], target = 6\n" +
                "输出：[1,2]\n" +
                "示例 3：\n" +
                "\n" +
                "输入：nums = [3,3], target = 6\n" +
                "输出：[0,1]\n" +
                " \n" +
                "\n" +
                "提示：\n" +
                "\n" +
                "2 <= nums.length <= 104\n" +
                "-109 <= nums[i] <= 109\n" +
                "-109 <= target <= 109\n" +
                "只会存在一个有效答案\n" +
                "进阶：你可以想出一个时间复杂度小于 O(n2) 的算法吗？\n" +
                "\n" +
                "来源：力扣（LeetCode）\n" +
                "链接：https://leetcode.cn/problems/two-sum\n" +
                "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。");
        System.out.println(problem.getDescription());
        problem.setTemplateCode("class Solution {\n" +
                "    public int[] twoSum(int[] nums, int target) {\n" +
                "\n" +
                "    }\n" +
                "}");
        problem.setTestCode("    public int[] mySolution(int[] nums, int target) {\n" +
                "        int[] indexs = new int[2];\n" +
                "\n" +
                "        HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();\n" +
                "        for(int i = 0; i < nums.length; i++){\n" +
                "            if(hash.containsKey(nums[i])){\n" +
                "                indexs[0] = i;\n" +
                "                indexs[1] = hash.get(nums[i]);\n" +
                "                return indexs;\n" +
                "            }\n" +
                "            hash.put(target-nums[i],i);\n" +
                "        }\n" +
                "        return indexs;\n" +
                "    }\n" +
                "\n" +
                "    public boolean compareTo(int[] result, int[] myResult) {\n" +
                "        Arrays.sort(myResult);\n" +
                "        Arrays.sort(result);\n" +
                "        if (result.length != myResult.length) {\n" +
                "            return false;\n" +
                "        } else {\n" +
                "            boolean flag = true;\n" +
                "            for (int i = 0; i < result.length; i++) {\n" +
                "                if (result[i] != myResult[i]) {\n" +
                "                    return false;\n" +
                "                }\n" +
                "            }\n" +
                "            return true;\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "    public static void main(String[] args) {\n" +
                "        Solution solution = new Solution();\n" +
                "        //testcase1\n" +
                "        int[] nums = {2,7,11,15};\n" +
                "        int target = 9;\n" +
                "        int[] result = solution.twoSum(nums, target);\n" +
                "        int[] myResult = solution.mySolution(nums, target);\n" +
                "        boolean flag = solution.compareTo(result, myResult);\n" +
                "        if (flag) {\n" +
                "            System.out.println(\"testcase1 OK\");\n" +
                "        } else {\n" +
                "            System.out.println(\"testcase1 failed!\");\n" +
                "        }\n" +
                "        //testcase2\n" +
                "        nums = new int[]{3, 2, 4};\n" +
                "        target = 6;\n" +
                "        result = solution.twoSum(nums,target);\n" +
                "        myResult = solution.mySolution(nums, target);\n" +
                "        flag = solution.compareTo(result, myResult);\n" +
                "        if (flag) {\n" +
                "            System.out.println(\"testcase2 OK\");\n" +
                "        } else {\n" +
                "            System.out.println(\"testcase2 failed!\");\n" +
                "        }\n" +
                "    }");

        int ret = ProblemDAO.insertProblem(problem);
        if (ret == 1) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }
    }
}
